from application import db, login_manager
from flask_login import UserMixin  # 用来实现验证
import datetime


# 用户角色关联表
class UsersRole(db.Model):
    # 表名
    __tablename__ = 'users_role'
    # 评分用户
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True)
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'), primary_key=True)


# 用户表
class Users(UserMixin, db.Model):

    # 表名
    __tablename__ = 'users'

    # 主键
    id = db.Column(db.Integer, primary_key=True)
    # 用户名,mysql唯一索引不能查过128位
    username = db.Column(db.String(128), nullable=False, unique=True)
    # 密码
    password = db.Column(db.String(512), nullable=False)
    # 姓名 ; index 索引
    fullname = db.Column(db.String(128), nullable=False, index=True)
    # 状态(启用:1, 停用: 0)
    status = db.Column(db.SmallInteger, default=1, nullable=False)
    # 创建时间(utcnow-格林威治时间); index 索引
    created_time = db.Column(db.DateTime, default=datetime.datetime.now, nullable=False, index=True)

    # 建立外键指向班级主键
    class_id = db.Column(db.Integer, db.ForeignKey('class.id'))

    # 当期听课状态
    current_listen_status = db.Column(db.String(32), default='go', nullable=True, index=True)
    # 用户头像
    avatar = db.Column(db.String(128), nullable=True)

    #
    roles = db.relationship('UsersRole', foreign_keys=[UsersRole.user_id], backref=db.backref('users', lazy='joined'),
                            lazy='dynamic', cascade='all, delete-orphan')

    def __repr__(self):
        return self.fullname


# Flask-Login,要求程序实现一个回调函数,使用指定的标识符加载用户
@login_manager.user_loader
def load_user(user_id):
    return Users.query.get(int(user_id))


# 角色权限关联表
class RolePermission(db.Model):
    # 表名
    __tablename__ = 'role_permission'

    # 角色
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'), primary_key=True)

    permission_id = db.Column(db.Integer, db.ForeignKey('permission.id'), primary_key=True)


# 角色表
class Role(db.Model):
    # 表名
    __tablename__ = 'role'

    # 主键
    id = db.Column(db.Integer, primary_key=True)
    # 角色名(办公室人员)
    name = db.Column(db.String(128), nullable=False, unique=True)
    # 角色编码(BGS)
    code = db.Column(db.String(32), nullable=False, unique=True)

    # 拥有该角色的用户
    users = db.relationship('UsersRole', foreign_keys=[UsersRole.role_id], backref=db.backref('role', lazy='joined'),
                               lazy='dynamic', cascade='all, delete-orphan')

    # 该角色拥有的权限
    permissions = db.relationship('RolePermission', foreign_keys=[RolePermission.role_id],
                                  backref=db.backref('role', lazy='joined'),
                                  lazy='dynamic', cascade='all, delete-orphan')


# 权限表
class Permission(db.Model):
    # 表名
    __tablename__ = 'permission'

    # 主键
    id = db.Column(db.Integer, primary_key=True)
    # 权限名(用车申请)
    name = db.Column(db.String(128), nullable=False, unique=True)
    # 权限编码(YCSQ-XZ) (/use_car, POST)
    code = db.Column(db.String(32), nullable=False, unique=True)

    # 该角色拥有的权限
    roles = db.relationship('RolePermission', foreign_keys=[RolePermission.permission_id], backref=db.backref('permission', lazy='joined'),
                            lazy='dynamic', cascade='all, delete-orphan')
